layui.use(['table', 'admin', 'ax','simTool','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var simTool = layui.simTool;
    var laydate = layui.laydate;

    /**
     * 农场视频播放时间管理
     */
    var FactoryVideoTime = {
        tableId: "factoryVideoTimeTable"
    };
    var sim = new simTool();
    sim.updateButton("/factoryVideoTime/editStatus",FactoryVideoTime.tableId,"status",[1,0]);

    /**
     * 初始化表格的列
     */
    FactoryVideoTime.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'factoryVideoTimeName', sort: true, title: '农场摄像头'},
            {field: 'showTime', sort: true, title: '此视频播放时间段'},
            {field: 'url', sort: true, title: '链接'},
            {field: 'status', sort: true, title: '状态',templet:'#statusTpl'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FactoryVideoTime.search = function () {
        var queryData = {};
        queryData['status'] = $("#status").val();
        table.reload(FactoryVideoTime.tableId, {where: queryData,
            curr:{page:1}
        });
    };

    /**
     * 弹出添加对话框
     */
    FactoryVideoTime.openAddDlg = function () {
        admin.putTempData('formOk', false);
        layui.admin.open({
            type: 2,
            maxmin: true,
            area:["100%","100%"],
            title: '添加时间区间表',
            content: Feng.ctxPath + '/factoryVideoTime/add?factoryVideoId='+urlParam,
            end: function () {
                admin.getTempData('formOk') && table.reload(FactoryVideoTime.tableId);
            }
        });
    };


    /**
     * 导出excel按钮
     */
    FactoryVideoTime.exportExcel = function () {
        var checkRows = table.checkStatus(FactoryVideoTime.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };


    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    FactoryVideoTime.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            maxmin:true,
            area:["50%","50%"],
            title: '修改时间区间表',
            content: Feng.ctxPath + '/factoryVideoTime/edit?id=' + data.id+"&factoryVideoId="+urlParam,
            end: function () {
                admin.getTempData('formOk') && table.reload(FactoryVideoTime.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FactoryVideoTime.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/factoryVideoTime/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(FactoryVideoTime.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };


    var urlParam = Feng.getUrlParam("factoryVideoId");
    $("#factoryVideoId").val(urlParam);
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FactoryVideoTime.tableId,
        url: Feng.ctxPath + '/factoryVideoTime/list?factoryVideoId='+urlParam,
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: FactoryVideoTime.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FactoryVideoTime.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        FactoryVideoTime.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        FactoryVideoTime.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + FactoryVideoTime.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            FactoryVideoTime.openEditDlg(data);
        } else if (layEvent === 'delete') {
            FactoryVideoTime.onDeleteItem(data);
        }
    });

    laydate.render({
        elem: '#showTime',
        type:'time',
        range: true,
        done: function(value, date, endDate){
            console.log(value); //得到日期生成的值，如：2017-08-18
            console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
            console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
            if(date.hours>=endDate.hours){
                Feng.error("起止时间应从小到大");
                var element = document.getElementById("showTime");
                element.setvalue("")
            }
        }
    });
});
